<?php

class Budget_manager extends Member_Controller {

    function __construct() {
        parent::__construct();

        //Set folder to load view (under View for)
        $this->layouts->set_view_folder('budget_manager');

        //Load model
        $this->load->model('budget_manager_model');

        //load js and css
        $this->layouts->set_css('budget/budget_manager');
        $this->layouts->set_js('budget_manager/my_budget');
        $this->layouts->set_js('budget_manager/update_budget');
        $this->layouts->set_js('budget_manager/new_budget');
    }

    function index() {
        $this->my_budget();
    }

    function my_budget() {
        $getMyBudget = $this->budget_manager_model->getMyBudget($this->user_id);

        $count = count($getMyBudget);

        for ($counter = 0; $counter < $count; $counter++) {
            $budget_id = $getMyBudget[$counter]->budget_id;

            $get_total_daily = $this->budget_manager_model->get_total_daily($budget_id);
            $daily = $get_total_daily[0]->daily_expenses;

            $get_total_ticket = $this->budget_manager_model->get_total_ticket($budget_id);
            
            $ticket = $get_total_ticket[0]->ticket_cost;

            $get_total_schFees = $this->budget_manager_model->get_total_schFees($budget_id);

            $schFees = $get_total_schFees[0]->schFees;

            $grand_total = $daily * 12 + $ticket + $schFees * 2;

            $budget_name = $getMyBudget[$counter]->budget_name;
            $budget_name_wL = "<a href='" . site_url('budget_manager/get_budget_specific') . "?id=" . $budget_id . "&currentItem=4'>" . $budget_name . "</a>";
            $budget_id_check = "<input type='checkbox' name='my_budget_ids' value='" . $budget_id . "' />";
            $lifestyle = $getMyBudget[$counter]->lifestyle_type;
            $sch_type = $getMyBudget[$counter]->institution_type_desc;
            $country = $getMyBudget[$counter]->country_name;
            $course = $getMyBudget[$counter]->course_name;
            $data['myBudget'][$counter] = array($budget_id_check, $budget_name_wL, $lifestyle, $sch_type, $country, $course, $grand_total);
        }

        $this->layouts->view('my_budget', $data);
    }

    function delete_budget() {
        $budget_id = $this->input->post('budget_id');
        $this->budget_manager_model->deleteMyBudget($budget_id);
    }

    function get_budget_specific() {
        $data['insType'] = $this->budget_manager_model->insType();
        $data['countryList'] = $this->budget_manager_model->countryList();
        $data['courseList'] = $this->budget_manager_model->courseList();
        $data['lifestyleList'] = $this->budget_manager_model->lifestyleList();
        $get_budget_id = $_GET['id'];
        $arrayBudgetData = $this->budget_manager_model->getMyBudgetSpecific($get_budget_id);
        $arrayBudgetDataAmount = $this->budget_manager_model->getMyBudgetSpecificAmount($get_budget_id);
        $data['get_lifestyle'] = $arrayBudgetData[0]->lifestyle_type;
        $data['get_country'] = $arrayBudgetData[0]->country_id;
        $data['get_sch_type'] = $arrayBudgetData[0]->inst_type;
        $data['get_course'] = $arrayBudgetData[0]->course_genre;
        $data['get_budget_name'] = $arrayBudgetData[0]->budget_name;
        $data['get_rent_selected'] = $arrayBudgetDataAmount[1]->amount;
        $data['get_food_selected'] = $arrayBudgetDataAmount[0]->amount;
        $data['get_transport_selected'] = $arrayBudgetDataAmount[2]->amount;
        $data['get_travel_times'] = $arrayBudgetDataAmount[3]->amount;
        $data['get_ticket_type'] = $arrayBudgetDataAmount[3]->ticket_type;
        $this->layouts->view('update_budget_content', $data);
    }

    function updateBudget() {
        $budget_id = $_GET['id'];
        $this->budget_manager_model->updateMyBudget($budget_id);
        $this->budget_manager_model->updateMyBudgetComponent($budget_id);
        redirect('budget_manager/my_budget');
    }

    function new_budget() {
        $data['insType'] = $this->budget_manager_model->insType();
        $data['countryList'] = $this->budget_manager_model->countryList();
        $data['courseList'] = $this->budget_manager_model->courseList();
        $data['lifestyleList'] = $this->budget_manager_model->lifestyleList();

        $this->layouts->view('new_budget', $data);
    }

    function getSchoolFee() {
        $country = $this->input->post('country');
        $inst_type = $this->input->post('inst_type');
        $courseID = $this->input->post('course');
        $school_fee = $this->budget_manager_model->getSchoolFee($country, $inst_type, $courseID);
        //echo $school_fee[0]->minimum_amount;
    }

    function getMonthlyExpenses() {
        $lifestyle = $this->input->post('lifestyle');
        $country = $this->input->post('country');
        $rental = $this->budget_manager_model->getMonthlyExpenses($lifestyle, 'rent', $country);
        $foodCost = $this->budget_manager_model->getMonthlyExpenses($lifestyle, 'food', $country);
        $transport = $this->budget_manager_model->getMonthlyExpenses($lifestyle, 'transport', $country);
        //echo $rental[0]->minimum_amount . "|" . $rental[0]->maximum_amount . "|" . $rental[0]->recommended_amount;
        //echo "|" . $foodCost[0]->minimum_amount . "|" . $foodCost[0]->maximum_amount . "|" . $foodCost[0]->recommended_amount;
        //echo "|" . $transport[0]->minimum_amount . "|" . $transport[0]->maximum_amount . "|" . $transport[0]->recommended_amount;
        //echo "|" . $rental[0]->budget_component_id . "|" . $foodCost[0]->budget_component_id . "|" . $transport[0]->budget_component_id;
        echo "100|500|200|100|200|150|500|700|520|1|1|1";
    }

    function getTicketCost() {
        $country = $this->input->post('country');
        $ticketClass = $this->input->post('ticketClass');
        $fly_times = $this->input->post('fly_times');
        $ticketCost = $this->budget_manager_model->getTicketCost($country, $ticketClass, 'travel', '1');
        //$finalCost = $ticketCost[0]->minimum_amount * $fly_times;
        //echo $finalCost;
        //echo "|";
        //echo $ticketCost[0]->budget_component_id;
    }

    function insertBudget() {
        $this->load->model('budget_manager_model');
        $this->budget_manager_model->insertBudgetComponent($this->budget_manager_model->insertBudget($this->user_id));
        redirect('budget_manager/my_budget');
    }

}
